package com.zmsk.zmio.handler;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;

import com.seeku.android.BoardManager;
import com.seeku.android.utils.FileUtil;
import com.zmsk.zmio.inter.IIOHandler;

public class CLHSHandler implements IIOHandler {

    BoardManager mBoardManager;
    private final String gpioPath = "/sys/class/custom_class/custom_dev/";

    private static final int MSG_RESTART_APP = 0x01;
    private Context mContext;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Log.d("CLHSHandler", "handle restart");
            Intent intent = new Intent();
            intent.setClassName("com.zmsk.eface", "com.zmsk.zmguard.CameraSurfaceActivity");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        }
    };

    public CLHSHandler (Context context) {
        mContext = context;
        initIOControl(context);
    }

    @Override
    public void initIOControl(Context context) {
        mBoardManager = BoardManager.getInstance();
        mBoardManager.init(context);
    }

    @Override
    public void stopIOControl(Context context) {

    }

    @Override
    public void noticeGateOpen(Context context, String idNumber) {
        mBoardManager.setGate(true);
    }

    @Override
    public void noticeGateClose(Context context) {
        mBoardManager.setGate(false);
    }

    @Override
    public void noticeWhiteLedOn(Context context) {
        mBoardManager.setWhite(true);
    }

    @Override
    public void noticeWhiteLedOff(Context context) {
        mBoardManager.setWhite(false);
    }

    @Override
    public void noticeInfraredLedOn(Context context) {
            //set(RED_LED_CTL,enable);
            String path = gpioPath+"/ir";
            FileUtil.Write2File(path,1);
    }

    @Override
    public void noticeInfraredLedOff(Context context) {
        String path = gpioPath+"/ir";
        FileUtil.Write2File(path,0);

    }

    @Override
    public void noticeRedLedOn(Context context) {
        mBoardManager.setRed(true);
    }

    @Override
    public void noticeRedLedOff(Context context) {
        mBoardManager.setRed(false);
    }

    @Override
    public void noticeGreenLedOn(Context context) {
        mBoardManager.setGreen(true);
    }

    @Override
    public void noticeGreenLedOff(Context context) {
        mBoardManager.setGreen(false);
    }

    @Override
    public void handleSlientInstall(String path) {
        Log.d("CLHSHandler", "handleSlientInstall:path:" + path);
        if (!TextUtils.isEmpty(path)) {
            mBoardManager.appSilentInstall(path);
            mHandler.sendEmptyMessageDelayed(MSG_RESTART_APP, 20 * 1000);
        }
        Log.d("CLHSHandler", "handleSlientInstall, finish");
    }
}
